import { Controller, Get, Post, Body, Patch, Param, Delete, UseFilters, Query, UseGuards } from '@nestjs/common';
import { ApiBearerAuth, ApiOperation, ApiResponse, ApiTags } from '@nestjs/swagger';
import { TypeormFilter } from 'src/common/filters/typeorm.filter';
import { JwtGuard } from 'src/common/guards/jwt.guard';
import { ArticleService } from './article.service';
import { CreateArticleDto } from './dto/create-article.dto';
import { GetArticleDto } from './dto/get-article.dto';
import { UpdateArticleDto } from './dto/update-article.dto';
import { ArticleResultVo } from './vo/article.result.vo';

@ApiTags('文章')
@Controller('article')
@UseFilters(new TypeormFilter())
export class ArticleController {
  constructor(private readonly articleService: ArticleService) {}

  @Post()
  @ApiOperation({summary:'新增文章'})
  @ApiResponse({type:Number})
  @ApiBearerAuth()
  @UseGuards(JwtGuard)
  create(@Body() createArticleDto: CreateArticleDto) {
    return this.articleService.create(createArticleDto);
  }

  @Get()
  @ApiOperation({summary:'文章列表'})
  @ApiResponse({type:ArticleResultVo})
  @ApiBearerAuth()
  @UseGuards(JwtGuard)
  findAll(@Query() query:GetArticleDto) {
    return this.articleService.findAll(query,true);
  }

  @Get('user')
  @ApiOperation({summary:'文章列表（博客端）'})
  @ApiResponse({type:ArticleResultVo})
  findAllUser(@Query() query:GetArticleDto) {
    return this.articleService.findAll(query,false);
  }

  @Get(':id')
  @ApiOperation({summary:'文章详情'})
  @ApiResponse({type:ArticleResultVo})
  @ApiBearerAuth()
  @UseGuards(JwtGuard)
  findOne(@Param('id') id: string) {
    return this.articleService.findOne(+id,true);
  }

  @Get('user/:id')
  @ApiOperation({summary:'文章详情（博客端）'})
  @ApiResponse({type:ArticleResultVo})
  @ApiBearerAuth()
  findOneUser(@Param('id') id: string) {
    return this.articleService.findOne(+id,false);
  }

  @Patch(':id')
  @ApiOperation({summary:'文章修改'})
  @ApiResponse({type:null})
  @ApiBearerAuth()
  @UseGuards(JwtGuard)
  update(@Param('id') id: string, @Body() updateArticleDto: UpdateArticleDto) {
    return this.articleService.update(+id, updateArticleDto);
  }

  @Delete(':id')
  @ApiOperation({summary:'删除文章'})
  @ApiResponse({type:null})
  @ApiBearerAuth()
  @UseGuards(JwtGuard)
  remove(@Param('id') id: string) {
    return this.articleService.remove(+id);
  }
}
